package com.atgiogi.meiniantour.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.meinian.constant.MessageConstant;
import com.atguigu.meinian.constant.RedisMessageConstant;
import com.atguigu.meinian.service.OrderSerivce;
import com.atguigu.meinian.util.MeiNianUtils;
import com.atguigu.meinian.util.SMSUtils;
import com.atguigu.meinian.util.ValidateCodeUtils;
import com.atguigu.meinian.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zbystart
 * @create 2021-06-24 14:43
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderSerivce orderSerivce;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 发送短信验证码
     * @param
     * @return
     */
    @PostMapping("/sendCode")
    public R sendCode(@RequestBody Map<String,String> map) {
        String telephone = map.get("telephone");
        String codeKey = telephone + ":" + RedisMessageConstant.SENDTYPE_ORDER;

//        // 先从redis中获取一下验证码
//        String codeFromDb = jedisPool.getResource().get(codeKey);
//        if (codeFromDb != null) {
//            // 已经发送验证码了就不要再发送了
//            return R.ok(MessageConstant.SEND_VALIDATECODE_SUCCESS,null);
//        }

        // TODO 在一天内同一个手机号限制发送验证码的次数？


        try {
            // 发送短信验证码
            MeiNianUtils.sendCode(telephone,4,jedisPool,RedisMessageConstant.SENDTYPE_ORDER,5 * 60);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        return R.ok(MessageConstant.SEND_VALIDATECODE_SUCCESS,null);
    }

    /**
     * 保存预约信息
     * @param map
     * @return
     */
    @PostMapping("/submitOrderInfo")
    public R saveOrderInfo(@RequestBody Map<String,String> map) {
        // 1.获取手机号检查验证码是否正确
        String telephone = map.get("telephone");
        String code = map.get("validateCode");
        Map<String, String> codeResult = MeiNianUtils.checkCode(telephone,code,jedisPool,RedisMessageConstant.SENDTYPE_ORDER);
        if (codeResult.get(MessageConstant.CODE_VERIFY_RESULT_KEY).equals(MessageConstant.CODE_VERIFY_SUCCESS)) {
            // 验证码正确
            // 2.正确的话，在调用service保存预约信息
            try {
                R r = orderSerivce.saveOrder(map);
                return r;
            } catch (RuntimeException e) {
                e.printStackTrace();
                return R.error(e.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
                return R.error(MessageConstant.ORDER_FAIL);
            }
        } else {
            // 验证码错误
            return R.error(codeResult.get(MessageConstant.CODE_VERIFY_MESSAGE_Key));
        }
    }

    @GetMapping("findById")
    public R findOrderInfoByOrderId(Integer id) {
        try {
            Map<String,Object> orderInfoMap =  orderSerivce.queryOrderInfoByOrderId(id);
            return R.ok(MessageConstant.QUERY_ORDER_SUCCESS,orderInfoMap);
        } catch (Exception e) {
            return R.error(MessageConstant.QUERY_ORDER_FAIL);
        }

    }

}
